/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import DAO.*;
import POJO.*;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author ben
 */
public class form_VeTau extends javax.swing.JInternalFrame {

    /**
     * Creates new form form_VeTau
     */
    public form_VeTau() throws SQLException {
        initComponents();
        
         Load();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCapNhat = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtMaVe = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtGiaVe = new javax.swing.JTextField();
        btnThoat = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbVeTau = new javax.swing.JTable();
        cbMaChuyen = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cbMaGhe = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        btnCapNhat.setText("Cập Nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        jLabel8.setText("Giá Vé:");

        jLabel5.setText("QUẢN LÝ VÉ TÀU");

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        jLabel3.setText("Mã Vé:");

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        tbVeTau.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Mã Vé", "Mã Ghế", "Mã Chuyến", "Giá Vé"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbVeTau.setColumnSelectionAllowed(true);
        tbVeTau.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbVeTauMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbVeTau);
        tbVeTau.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        cbMaChuyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Mã chuyến:");

        cbMaGhe.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Mã ghế:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40))))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(257, 257, 257)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbMaGhe, 0, 159, Short.MAX_VALUE)
                            .addComponent(txtMaVe))
                        .addGap(76, 76, 76)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtGiaVe)
                            .addComponent(cbMaChuyen, 0, 141, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMaVe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(cbMaChuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(cbMaGhe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(txtGiaVe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnThoat)
                    .addComponent(btnCapNhat))
                .addGap(17, 17, 17))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
DefaultComboBoxModel modelgn;
    DefaultComboBoxModel modelct;
    DAO_VeTau vt = new DAO_VeTau();
    DefaultTableModel model = new DefaultTableModel();
    
    @SuppressWarnings("empty-statement")
    public void Load() throws SQLException{
        List<POJO_VeTau> list = vt.LoadList();
        model = new DefaultTableModel(null, new Object[]{
            "Mã vé", "Mã ghế", "Mã Chuyến", "Giá vé"});
        for(POJO_VeTau veTau : list){
            model.addRow(new Object[]{
                veTau.getMAVE(),
                veTau.getMAGHE(),
                veTau.getMACHUYEN(),
                veTau.getGIAVE()
            });
        };
        tbVeTau.setModel(model);
        
        DAO_GheNgoi gn = new DAO_GheNgoi();
        List<POJO_GheNgoi> gheNgoi = gn.LoadList();
        modelgn = new DefaultComboBoxModel(gheNgoi.toArray());
        cbMaGhe.setModel(modelgn);
        
        DAO_ChuyenTau ct = new DAO_ChuyenTau();
        List<POJO_ChuyenTau> chuyenTau = ct.LoadList();
        modelct = new DefaultComboBoxModel(chuyenTau.toArray());
        cbMaChuyen.setModel(modelct);
    }
    
    private void tbVeTauMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbVeTauMouseClicked
        int index = tbVeTau.getSelectedRow();
        
        txtMaVe.setText(String.valueOf(model.getValueAt(index, 0)));
        modelgn.setSelectedItem(String.valueOf(model.getValueAt(index, 1)));
        modelct.setSelectedItem(String.valueOf(model.getValueAt(index, 2)));
        txtGiaVe.setText(String.valueOf(model.getValueAt(index, 3)));
    }//GEN-LAST:event_tbVeTauMouseClicked
    
    public boolean TestEmpty(){
        return !txtMaVe.getText().isEmpty() && !txtGiaVe.getText().isEmpty();
    }
    
    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
       if(TestEmpty() == true){
            POJO_VeTau vetau = new POJO_VeTau();
            
            vetau.setMAVE(txtMaVe.getText());
            vetau.setMAGHE(modelgn.getSelectedItem().toString());
            vetau.setMACHUYEN(modelct.getSelectedItem().toString());
            vetau.setGIAVE(Integer.valueOf(txtGiaVe.getText()));
            
            vt.themVeTau(vetau);
            
            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_GheNgoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Thêm thành công");
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        int index = tbVeTau.getSelectedRow();
        
        if(index > 0){
            POJO_VeTau vetau = new POJO_VeTau();
            vetau.setMAVE(txtMaVe.getText());
            
            vt.xoaVeTau(vetau);
            
            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_GheNgoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Xóa thành công");
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng chọn dòng cần xóa");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
         if(TestEmpty() == true){
            POJO_VeTau vetau = new POJO_VeTau();
            
            vetau.setMAVE(txtMaVe.getText());
            vetau.setMAGHE(modelgn.getSelectedItem().toString());
            vetau.setMACHUYEN(modelct.getSelectedItem().toString());
            vetau.setGIAVE(Integer.valueOf(txtGiaVe.getText()));
            
            vt.capNhatVeTau(vetau);
            
            try {
                Load();
            } catch (SQLException ex) {
                Logger.getLogger(form_GheNgoi.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            JOptionPane.showMessageDialog(null, "Cập nhật thành công");
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng nhập đầy đủ thông tin");
         }
    }//GEN-LAST:event_btnCapNhatActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cbMaChuyen;
    private javax.swing.JComboBox cbMaGhe;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbVeTau;
    private javax.swing.JTextField txtGiaVe;
    private javax.swing.JTextField txtMaVe;
    // End of variables declaration//GEN-END:variables
}
